package cn.wolfcode.web.controller;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.web.feign.ProductFeignApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by lanxw
 * 秒杀商品信息查询
 */
@RestController
@RequestMapping("/seckillProduct")
@Slf4j
public class SeckillProductController {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private ProductFeignApi productFeignApi;
    @RequestMapping("/queryByTimeForJob")
    public Result<List<SeckillProductVo>> queryByTimeForJob(Integer time){
        List<SeckillProductVo> seckillProductVoList = seckillProductService.queryByTimeFromCache(time);
        return Result.success(seckillProductVoList);
    }

    @RequestMapping("/queryByTime")
    public Result<List<SeckillProductVo>> queryByTime(Integer time) {
//    List<SeckillProductVo> seckillProductVoList= seckillProductService.queryByTime(time);
//        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
//
//        ArrayList<Long> productIdList = new ArrayList<>();
//        for (SeckillProduct seckillProduct : seckillProducts) {
//            productIdList.add(seckillProduct.getProductId());
//
//        }
//        Result<List<Product>> result = productFeignApi.queryByIds(productIdList);
//        if (result==null||result.hasError()) {
//            throw new BusinessException(SeckillCodeMsg.PRODUCT_SERVER_ERROR);
//        }
//        List<Product> productList=result.getData();
//        Map<Long, Product> map = new HashMap<>();
//        for (Product product : productList) {
//            map.put(product.getId(),product);
//        }
//        for (SeckillProduct seckillProduct : seckillProducts) {
//
//            Long productId=seckillProduct.getProductId();
//            Product product = map.get(productId);
//            SeckillProductVo vo = new SeckillProductVo();
//            BeanUtils.copyProperties(product,vo);
//            seckillProductVoList.add(vo);
//        }
//        return seckillProductVoList;
//    }
        List<SeckillProductVo> seckillProductVoList = seckillProductService.queryByTime(time);
        return Result.success(seckillProductVoList);
    }
    @RequestMapping("/find")
    public Result<SeckillProductVo> find(Integer time,Long seckillId){
        SeckillProductVo vo=seckillProductService.findFromCache(time,seckillId);
    return Result.success(vo);
    }
}